//
//  UrbanForgetPasswordViewController.m
//  UrbanManagement
//
//  Created by Can on 18/4/10.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanForgetPasswordViewController.h"
#import "UrbanRegistTextFild.h"

@interface UrbanForgetPasswordViewController ()

@property (nonatomic,strong)NSString *phoneStr;//手机号
@property (nonatomic,strong)NSString *codeStr;//验证码
@property (nonatomic,strong)NSString *nameStr;//姓名
@property (nonatomic,strong)NSString *passStr;//密码
@property (nonatomic,strong)UrbanRegistTextFild *msgTextFild;//验证码

@property (nonatomic,strong)NSTimer *timer;//定时器
@property (nonatomic,assign)int s;//秒

@end

@implementation UrbanForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleWithTitle:@"忘记密码"];
    
    [self creatView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

-(void)creatView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 5*screenW_6)];
    lineView.backgroundColor = Color(247, 247, 247);
    [self.view addSubview:lineView];
    
    
    //手机号
    UrbanRegistTextFild *phoneTextFild= [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, 5*screenW_6, 0, 0) lableName:@"手机号" placeholder:@"请输入手机号" keyboardType:UIKeyboardTypeNumberPad maxNum:11 type:UrbanRegistTextFildPhoneType];
    [self.view addSubview:phoneTextFild];
    phoneTextFild.UrbanRegistTextFildblock = ^(NSString *phone){
        self->_phoneStr = phone;
    };
    
    //验证码
    self.msgTextFild = [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, CGRectGetMaxY(phoneTextFild.frame) + 5*screenW_6, 0, 0) lableName:@"验证码" placeholder:@"请输入验证码" keyboardType:UIKeyboardTypeNumberPad maxNum:0 type:UrbanRegistTextFildMsgType];
    [self.view addSubview:self.msgTextFild];
    [self.msgTextFild.btn addTarget:self action:@selector(sendMSg:) forControlEvents:UIControlEventTouchUpInside];
    self.msgTextFild.UrbanRegistTextFildblock = ^(NSString *msg){
        self->_codeStr = msg;
    };
    
    //密码
    UrbanRegistTextFild *passwordTextFild = [[UrbanRegistTextFild alloc]initWithFrame:CGRectMake(15*screenW_6, CGRectGetMaxY(self.msgTextFild.frame) + 5*screenW_6, 0, 0) lableName:@"密码" placeholder:@"请输入密码" keyboardType:UIKeyboardTypeDefault maxNum:0 type:UrbanRegistTextFildPassWordType];
    [self.view addSubview:passwordTextFild];
    passwordTextFild.UrbanRegistTextFildblock = ^(NSString *msg){
        self->_passStr = msg;
    };
    
    //提交
    UIButton *registBtn = [TomamaUI createButton:0 Frame:CGRectMake(15*screenW_6, CGRectGetMaxY(passwordTextFild.frame) + 25*screenW_6, SCREENW - 30*screenW_6, 45*screenW_6) targ:self sel:@selector(submit) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:16*screenW_6] title:@"提交" image:nil backGroundImage:@"blue_button2"];
    registBtn.layer.cornerRadius = 3*screenW_6;
    registBtn.layer.masksToBounds = YES;
    [self.view addSubview:registBtn];
}

-(void)sendMSg:(UIButton *)sendBtn//发送验证码
{
    sendBtn.userInteractionEnabled = NO;
    if (_phoneStr.length==11) {//验证手机号
        _s = 60;
        [sendBtn setTitle:@"60s" forState:UIControlStateNormal];
        sendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        
        //        [HttpRequest PostrequestUrl:@"/jeesite/appInterface/registInterface/sendVerificationCode" Dic:@{@"mobile":_phoneStr,@"userType":@"6"} completion:^(NSDictionary *responseDic) {
        //            NSLog(@"====%@",responseDic[@"msg"]);
        //            if (![responseDic[@"success"] boolValue]) {//请求成功
        //                [HudHelper showHudWithMessage:responseDic[@"msg"]?responseDic[@"msg"]:@"验证码发送失败" toView:self.view];
        //                [sendBtn setTitle:@"重发" forState:UIControlStateNormal];
        //                sendBtn.userInteractionEnabled = YES;
        //                [_timer invalidate];
        //                _timer = nil;
        //            }
        //        }];
    }
    else//验证失败
    {
        [ToolViewVC addKeyAlterViewController:@"手机号码格式不正确" Controller:self];
        sendBtn.userInteractionEnabled = YES;
    }
}

-(void)onTimer//定时器
{
    _s--;
    NSString *timeStr = [NSString stringWithFormat:@"%ds",_s];
    [_msgTextFild.btn setTitle:timeStr forState:UIControlStateNormal];
    if (_s==0)//重置按钮 定时器
    {
        [_msgTextFild.btn setTitle:@"重发" forState:UIControlStateNormal];
        _msgTextFild.btn.userInteractionEnabled = YES;
        [_timer invalidate];
        _timer = nil;
        _s = 60;
    }
}


- (void)submit{
    if (_phoneStr.length == 0) {
        
        [ToolViewVC addKeyAlterViewController:@"请输入手机号" Controller:self];
        
    }else if (_phoneStr.length != 11){
        
        [ToolViewVC addKeyAlterViewController:@"手机号输入有误" Controller:self];
        
    }else if (_codeStr.length == 0){
        
        [ToolViewVC addKeyAlterViewController:@"请输入验证码" Controller:self];
        
    }else if (_passStr.length == 0){
        
        [ToolViewVC addKeyAlterViewController:@"请输入密码" Controller:self];
        
    }else if (_passStr.length < 6){
        
        [ToolViewVC addKeyAlterViewController:@"密码不能小于6位" Controller:self];
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
